package cn.itcast.flink.trans;

import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoFlatMapFunction;
import org.apache.flink.util.Collector;

/**
 * Author itcast
 * Date 2022/1/13 9:18
 * Desc TODO
 */
public class ConnectDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<Integer> source1 = env.fromElements(1, 2, 3, 4, 5, 6);
        DataStreamSource<String> source2 = env.fromElements("hello", "str");
        //通过 connect 进行数据的合并
        ConnectedStreams<Integer, String> connect = source1.connect(source2);
        //对合流数据进行处理
        connect.flatMap(new CoFlatMapFunction<Integer, String, String>() {
            @Override
            public void flatMap1(Integer value, Collector<String> out) throws Exception {
                out.collect(value + "");
            }

            @Override
            public void flatMap2(String value, Collector<String> out) throws Exception {
                out.collect(value);
            }
        }).print();
        //执行流环境
        env.execute();
    }
}
